﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;

namespace TfsCommitMonitor.Tfs
{
	internal static class ProjectStore
	{
		#region Persistance
		internal static Boolean HasPersistedData()
		{
			return 
				IsolatedStorageFile.GetUserStoreForAssembly().FileExists(Configuration.IsolatedProjectCollectionFileName);
		}

		internal static void DeletePersistedData()
		{
			if (HasPersistedData())
				IsolatedStorageFile.GetUserStoreForAssembly().DeleteFile(Configuration.IsolatedProjectCollectionFileName);
		}

		internal static void PersistToDisk(this List<Project> projects)
		{
			using (var sw = new StreamWriter(Configuration.GetIsolatedStorageWriteStream(Configuration.IsolatedProjectCollectionFileName)))
				sw.Write(projects.Serialize());
		}

		internal static List<Project> LoadFromDisk()
		{
			using (var sr = new StreamReader(Configuration.GetIsolatedStorageReadStream(Configuration.IsolatedProjectCollectionFileName)))
				return
					sr.ReadToEnd().Deserialize<List<Project>>();
		}
		#endregion
	}
}
